
using UnityEngine;

public class CameraMove
{
    public float fixedYAngle = -90;
    public Transform followTarget;
    public Transform moveTarget;

    public void FreeMove(float fAngle, out float dirX, out float dirY, out float dirZ)
    {
        if (moveTarget == null)
        {
            dirX = 0;
            dirY = 0;
            dirZ = 0;
            return;
        }

        Vector3 vecDir = followTarget != null ? followTarget.rotation * Vector3.forward : Vector3.forward;
        vecDir.y = 0.0f;
        vecDir.Normalize();

        Matrix4x4 mat = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(new Vector3(0, fAngle, 0)), Vector3.one);
        vecDir = mat.MultiplyVector(vecDir);

        if (vecDir.Equals(Vector3.zero))
        {
            dirX = moveTarget.forward.x;
            dirY = moveTarget.forward.y;
            dirZ = moveTarget.forward.z;
            return;
        }

        vecDir.Normalize();
        RotateTowards(vecDir);

        dirX = vecDir.x;
        dirY = vecDir.y;
        dirZ = vecDir.z;
    }

    void RotateTowards(Vector3 dir)
    {
        if (dir == Vector3.zero) return;

        Quaternion rot = moveTarget.rotation;
        Quaternion toTarget = Quaternion.LookRotation(dir);

        rot = Quaternion.Slerp(rot, toTarget, 1);
        Vector3 euler = rot.eulerAngles;
        euler.z = 0;
        euler.x = 0;
        euler.y += fixedYAngle;
        rot = Quaternion.Euler(euler);

        moveTarget.rotation = rot;
    }
}
